package com.wujunshen.algorithm.leetcode.dynamic.programming;

/**
 * @author frank woo(吴峻申) <br>
 * email:<a href="mailto:frank_wjs@hotmail.com">frank_wjs@hotmail.com</a> <br>
 * @date 2022/8/6 01:44<br>
 */
public class 最小路径和 {
    public int minPathSum(int[][] grid) {
        for(int i = 0; i < grid.length; i++) {
            for(int j = 0; j < grid[0].length; j++) {
                //左边和上边都是矩阵边界时
                if(i == 0 && j == 0) {continue;}

                //只有左边是矩阵边界时
                if(i == 0) {
                    grid[i][j] = grid[i][j - 1] + grid[i][j];
                    continue;
                }

                //只有上边是矩阵边界时
                if(j == 0) {
                    grid[i][j] = grid[i - 1][j] + grid[i][j];
                    continue;
                }

                //左边和上边都不是矩阵边界时
                grid[i][j] = Math.min(grid[i - 1][j], grid[i][j - 1]) + grid[i][j];
            }
        }
        return grid[grid.length - 1][grid[0].length - 1];
    }
}
